package old.day20190320.c3p0;

import com.mchange.v2.c3p0.ComboPooledDataSource;


import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;

/**
 * @author DingZhenYun
 * @create 2019-03-21 10:54
 */
public class TestDemo1 {
    public static void main(String[] args) throws Exception {
       //  t1();
        t2();
        //t3();
    }

    private static void t3() throws Exception {
        Connection connection=JDBCTools.getConnection();
        System.out.println(connection.getClass());
    }

    private static void t2() throws SQLException {
        /**
         * 1. 创建 c3p0-config.xml 文件,
         * 参考帮助文档中 Appendix B: Configuation Files 的内容
         * 2. 创建 ComboPooledDataSource 实例；
         * DataSource dataSource =
         *			new ComboPooledDataSource("helloc3p0");
         * 3. 从 DataSource 实例中获取数据库连接.
         */
        DataSource dataSource=new ComboPooledDataSource("helloc3p0");
        System.out.println(dataSource.getConnection());
        ComboPooledDataSource comboPooledDataSource= (ComboPooledDataSource) dataSource;
        System.out.println(comboPooledDataSource.getConnection());

    }

    private static void t1() throws Exception {
        //使用c3p0连接池
        ComboPooledDataSource cpds = new ComboPooledDataSource();
        cpds.setDriverClass( "com.mysql.jdbc.Driver" );
        cpds.setJdbcUrl( "jdbc:mysql://localhost:3307/zhihu" );
        cpds.setUser("root");
        cpds.setPassword("123456");
        System.out.println(cpds.getConnection());
    }
}
